<template>
  <div>
    <el-row :gutter="10" class="mb8" style="border-bottom:1px solid #02A7F0;padding-bottom:5px;">
      <el-col>
          <span style="color:#02A7F0;font-size:18px;display:inline-block;padding-top:2px;"><b>催款信息</b></span>
          <div style="float:right;">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增</el-button>
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                :loading="exportLoading"
                @click="handleExport"
                >导出</el-button>
          </div>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="reminderList">
      <el-table-column label="催款日期" align="center" prop="reminderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reminderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="催款情况" align="center" prop="reminderContent" />
      <el-table-column label="催款人" align="center" prop="reminderUser" />
      <el-table-column label="录入人" align="center" prop="createBy" />
      <el-table-column label="录入时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  v-if="canEdit">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="total-info"></div>
    <!-- 添加或修改合同催款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="催款日期" prop="reminderDate">
          <el-date-picker clearable size="small"
            v-model="form.reminderDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择催款日期" style="width:100%;">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="催款人" prop="reminderUser">
          <el-input v-model="form.reminderUser" placeholder="请输入催款人" />
        </el-form-item>
        <el-form-item label="催款情况" prop="reminderContent">
          <el-input v-model="form.reminderContent" placeholder="请输入催款情况" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReminder, getReminder, delReminder, addReminder, updateReminder, exportReminder } from "@/api/business/reminder";

export default {
  name: "ReminderManage",
  props: {
    editConfig: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 合同催款表格数据
      reminderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999,
        contractId: null,
        reminderDate: null,
        reminderContent: null,
        reminderUser: null,
      },
      contractId: '',
      canEdit: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reminderDate: [
          { required: true, message: "催款日期不能为空", trigger: "blur" }
        ],
        reminderUser: [
          { required: true, message: "催款人不能为空", trigger: "blur" }
        ],
        reminderContent: [
          { required: true, message: "催款情况不能为空", trigger: "blur" }
        ],
      }
    };
  },
  mounted(){
      this.queryParams.contractId = this.editConfig.id
      this.contractId = this.editConfig.id
      this.canEdit = this.editConfig.canEdit
      this.getList()
  },
  methods: {
    /** 查询合同催款列表 */
    getList() {
      this.loading = true;
      listReminder(this.queryParams).then(response => {
        this.reminderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        reminderId: null,
        contractId: null,
        reminderDate: null,
        reminderContent: null,
        reminderUser: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.reminderId)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加催款信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const reminderId = row.reminderId || this.ids
      getReminder(reminderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改催款信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reminderId != null) {
            updateReminder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.contractId = this.contractId
            addReminder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const reminderIds = row.reminderId || this.ids;
      this.$confirm('是否确认删除催款数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReminder(reminderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同催款数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportReminder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
